package com.xn.app.muying.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xn.app.muying.activity.DengLuActivity;
import com.xn.app.muying.R;
import com.xn.app.muying.activity.HealthRecordsActivity;
import com.xn.app.muying.activity.JiuZhenActivity;
import com.xn.app.muying.activity.MainActivity;
import com.xn.app.muying.activity.MonitorSelectActivity;
import com.xn.app.muying.activity.OrderListActivity;
import com.xn.app.muying.activity.PersonalActivity;
import com.xn.app.muying.activity.TemperatureMonitoringActivity;
import com.xn.app.muying.activity.WebActivity;
import com.xn.app.muying.sqlite.DBManager;
import com.xn.app.muying.util.APIMethod;
import com.xn.app.muying.util.GetListviewHight;
import com.xn.app.muying.util.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 服务页面
 * Created by gaoge on 2017/1/5.
 */

public class MonitoringFragment extends Fragment implements View.OnClickListener{
    //预约挂号
    private RelativeLayout registered_layout=null;
    //健康档案
    private RelativeLayout file_layout=null;
    //专家医师
    private RelativeLayout doctor_layout=null;
    //营养中心
    private TextView shop_textview=null;
    //数据显示列表
    private ListView listview = null;
    //listview适配器
    private ListAdapter listAdapter = null;
    //检测功能集合
    private ArrayList<HashMap<String, String>> getData = null;
    private HashMap<String, String> map = null;
    //数据库
    private DBManager db = null;
    //获取用户
    private String userName = "";
    Intent intent = new Intent();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View wode = inflater.inflate(R.layout.monitoring_fragment, container, false);
        initView(wode);
        return wode;
    }

    /**
     * 初始化组件，实现功能
     *
     * @param wode
     */
    private void initView(View wode) {
        db = new DBManager(getContext());
        registered_layout = (RelativeLayout) wode.findViewById(R.id.registered_layout);
        file_layout = (RelativeLayout) wode.findViewById(R.id.file_layout);
        doctor_layout = (RelativeLayout) wode.findViewById(R.id.doctor_layout);
        shop_textview = (TextView) wode.findViewById(R.id.shop_textview);
        listview = (ListView) wode.findViewById(R.id.listview);
        //添加单击事件
        registered_layout.setOnClickListener(this);
        file_layout.setOnClickListener(this);
        doctor_layout.setOnClickListener(this);
        shop_textview.setOnClickListener(this);
        //listview点击事件
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //点击进入相应功能
                userName = db.select("tel");
                switch (i) {
                    //胎心监护
                    case 0:
                        if (userName != null && !userName.equals("") && !userName.equals("null")) {
                            String id = db.select("antenatalCareHospitalId");
                            if (id != null && !id.equals("") && !id.equals("null")) {
                                intent.setClass(getActivity(), MonitorSelectActivity.class);
                                getActivity().startActivity(intent);
//                                Intent intent = new Intent();
//                                ComponentName cmp = new ComponentName("com.tencent.mm","com.tencent.mm.ui.LauncherUI");
//                                intent.setAction(Intent.ACTION_MAIN);
//                                intent.addCategory(Intent.CATEGORY_LAUNCHER);
//                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                                intent.setComponent(cmp);
//                                startActivityForResult(intent, 0);
                            } else {
                                ToastUtil.showToast(getActivity(), "请先选择医院,然后进入胎心监护功能");
                                intent.putExtra("SIGN", "MonitorSelect");
                                intent.setClass(getContext(), JiuZhenActivity.class);
                                getActivity().startActivity(intent);
                            }
                        } else {
                            //如果没有登陆，调整到登陆页面
                            getActivity().startActivity(new Intent(getActivity(), DengLuActivity.class));
                        }
                        break;
                    //体温监测
                    case 1:
                        if (userName != null && !userName.equals("") && !userName.equals("null")) {
                            intent.setClass(getActivity(), TemperatureMonitoringActivity.class);
                            getActivity().startActivity(intent);
                        } else {
                            //如果没有登陆，调整到登陆页面
                            getActivity().startActivity(new Intent(getActivity(), DengLuActivity.class));
                        }
                        break;
                }
            }
        });
        //填充检测功能
        /**胎心监测*/
        getData = new ArrayList<>();
        map = new HashMap<>();
        map.put("title", "胎心监护");
        map.put("name", "智能胎心仪");
        map.put("data", "自主监测，随时了解胎儿胎心");
        map.put("icon", R.mipmap.taixinjianhu + "");
        map.put("background", R.mipmap.monitoring_fetal_bg + "");
        getData.add(map);
        /**体温监测*/
        map = new HashMap<>();
        map.put("title", "体温监测");
        map.put("name", "电子体温仪");
        map.put("data", "宝宝发烧看护，蹬被看护，洗澡看护");
        map.put("icon", R.mipmap.temperature + "");
        map.put("background",R.mipmap.monitoring_temperature_bg + "");
        getData.add(map);

        //设置适配器
        listAdapter = new ListAdapter(getContext(), getData);
        listview.setAdapter(listAdapter);
        //计算高度填充
        new GetListviewHight().setListViewHeight(listview);
    }

    /**
     * 单击事件
     * @param  v  控件
     */
    @Override
    public void onClick(View v) {
        userName = db.select("tel");
        switch (v.getId()){
            //预约挂号
            case R.id.registered_layout:
                if (userName != null && !userName.equals("") && !userName.equals("null")) {
                    intent.setClass(getActivity(), OrderListActivity.class);
                    startActivity(intent);
                } else {
                    //如果没有登陆，调整到登陆页面
                    startActivity(new Intent(MainActivity.mActivity, DengLuActivity.class));
                }
                break;
            //健康档案
            case R.id.file_layout:
                //检查是否登陆
                if (userName != null && !userName.equals("") && !userName.equals("null")) {
                    //判断用户是否填写过身份信息
                    if (db.select("identityId") != null && !db.select("identityId").equals("") && !db.select("identityId").equals("null")) {
                        startActivity(new Intent(MainActivity.mActivity, HealthRecordsActivity.class));
                    } else {
                        startActivity(new Intent(MainActivity.mActivity, PersonalActivity.class).putExtra("type", "1"));
                    }
                } else {
                    //如果没有登陆，调整到登陆页面
                    startActivity(new Intent(MainActivity.mActivity, DengLuActivity.class));
                }
                break;
            //专家医师
            case R.id.doctor_layout:
                intent.putExtra("SIGN", "introduction");
                intent.setClass(MainActivity.mActivity, JiuZhenActivity.class);
                startActivity(intent);
                break;
            //营养中心
            case R.id.shop_textview:
                intent.putExtra("title", "营养中心");
                intent.putExtra("url", APIMethod.WeiDian);
                intent.setClass(MainActivity.mActivity, WebActivity.class);
                startActivity(intent);
                break;
        }
    }

    /**
     * 列表适配器
     */
    private class ListAdapter extends BaseAdapter {
        LayoutInflater lf = null;
        ArrayList<HashMap<String, String>> getData = new ArrayList<HashMap<String, String>>();
        ViewHolder viewHolder = null;

        public ListAdapter(Context context,
                           ArrayList<HashMap<String, String>> getData) {
            lf = LayoutInflater.from(context);
            this.getData = getData;
        }

        @Override
        public int getCount() {
            return getData.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @SuppressLint("InflateParams")
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = lf.inflate(R.layout.monitoring_fragment_item, null);
                viewHolder.linearlayout= (LinearLayout) convertView.findViewById(R.id.linearlayout);
                viewHolder.title = (TextView) convertView.findViewById(R.id.title);
                viewHolder.image = (ImageView) convertView.findViewById(R.id.image);
                viewHolder.name = (TextView) convertView.findViewById(R.id.name);
                viewHolder.data = (TextView) convertView.findViewById(R.id.data);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.linearlayout.setBackgroundResource(Integer.parseInt(getData.get(position).get("background")));
            viewHolder.title.setText(getData.get(position).get("title"));
            viewHolder.image.setImageResource(Integer.parseInt(getData.get(position).get("icon")));
            viewHolder.name.setText(getData.get(position).get("name"));
            viewHolder.data.setText(getData.get(position).get("data"));
            return convertView;
        }
    }

    /**
     * 包装类
     */
    private static class ViewHolder {
        private TextView title;
        private ImageView image;
        private TextView name;
        private TextView data;
        private LinearLayout linearlayout;
    }
}
